package com.idol.service.impl;

import com.idol.annotation.Autowired;
import com.idol.annotation.Service;
import com.idol.annotation.Transaction;
import com.idol.dao.impl.AccountDaoImpl;
import com.idol.pojo.Account;
import com.idol.service.AccountService;
import com.idol.util.TransactionManager;

/**
 * @author Supreme_Sir
 * @version 1.0
 * @className AccountServiceImpl
 * @description
 * @date 2020/10/13 10:47
 **/
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDaoImpl accountDao;

    @Override
    @Transaction
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        try {
            Account fromAccount = accountDao.queryAccountByCardNo(fromCardNo);
            Account toAccount = accountDao.queryAccountByCardNo(toCardNo);

            fromAccount.setMoney(fromAccount.getMoney() - money);
            toAccount.setMoney(toAccount.getMoney() + money);

            accountDao.updateAccountByCardNo(fromAccount);
             int i = 1/0;
            accountDao.updateAccountByCardNo(toAccount);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
